package net.bitacademy.java23.utils;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileUpDownUtils {
	static public String saveFile(
			MultipartFile file, 
			String repositoryPath) throws Exception {
		
		String destFileName = getFileName(file.getOriginalFilename());
		File destFile = new File(repositoryPath + destFileName);
		FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
		return destFileName;
	}
	
	static private String getFileName(String originFileName) {
		return "file_" +
				System.currentTimeMillis() +
				"_" +
				(int)(Math.random() * 10000) +
			   originFileName.substring(originFileName.lastIndexOf("."));
	}
}
